//
//  OCBaseService.m
//  OCDemo
//
//  Created by 朱慕之 on 2021/12/28.
//

#import "OCBaseService.h"

@implementation OCBaseService

+ (instancetype)share{
    static dispatch_once_t t;
    static OCBaseService *service = nil;
    dispatch_once(&t, ^{
        service = [[OCBaseService alloc] init];
    });
    return service;
}

+ (BOOL)isConnectNetwork {
//    UdeskReachability *reach = [UdeskReachability reachabilityForInternetConnection];
//    if (reach.currentReachabilityStatus == UDNotReachable) {
//        return NO;
//    }
    return YES;
}

+ (NSString *)errorWithCode:(NSInteger)errorCode
{
    NSString *err = nil;
    if (errorCode) {
        switch (errorCode) {
            case OCNetworkErrorDefault:
                return @"数据返回失败，请重试。";
                break;
            case OCNetworkErrorTimeOut:
                return @"请求超时，请重试。";
                break;
            case OCNetworkErrorNoServer:
                return @"无法与服务器建立连接，请重试。";
                break;
            case OCNetworkErrorNoNetwork:
                return @"网络不给力，请检查网络连接。";
                break;
            default:
                return @"数据返回失败，请重试。";
                break;
        }
    }
    return err;
}


- (void)handleRequestSuccess:(NSDictionary *)dictionary
                requestModel:(RequestInfoModel *)requestInfoModel
                    sucBlock:(NetworkRequestSuccessBlock)sucBlock
                   failBlock:(NetworkRequestFailureBlock)failBlock{
    
    if (!dictionary || [[dictionary allKeys] count]==0) {
        if (failBlock) {
            NSError *error = [self errorWithErrorInfo:@"网络不给力" domain:@"网络不给力" code:RESULT_API_DATA_TYPE_HTTPERROR];
            failBlock (error,requestInfoModel);
        }
        return;
    }
    
    NSString *errorCode = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"errorCode"]];
    if (![dictionary objectForKey:@"errorCode"]) {
        errorCode = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"code"]];
    }
    if ([errorCode isEqualToString:@"0"]) {
        if (dictionary) {
            if (sucBlock){
                sucBlock (dictionary,requestInfoModel);
            }
        } else {
            if (failBlock) {
                NSError *error = [self errorWithErrorInfo:@"数据错误" domain:@"数据错误" code:RESULT_API_DATA_TYPE_ERROR];
                failBlock (error,requestInfoModel);
            }
        }
        return;
    }
    
    //其它异常：
    if (dictionary && ([dictionary objectForKey:@"errorCode"] || [dictionary objectForKey:@"code"] || [dictionary objectForKey:@"errorMessage"] || [dictionary objectForKey:@"msg"] || [dictionary objectForKey:@"message"])) {
        if (failBlock) {
            NSString *errorMessage = nil;
            if ([dictionary objectForKey:@"errorMessage"]) {
                errorMessage = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"errorMessage"]];
            }
            if ([dictionary objectForKey:@"msg"]) {
                errorMessage = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"msg"]];
            }
            if ([dictionary objectForKey:@"message"]) {
                errorMessage = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"message"]];
            }
            if (!ValidStr(errorMessage)) {
                errorMessage = @"网络不给力";
            }
            NSError *error = [self errorWithErrorInfo:errorMessage domain:errorMessage code:RESULT_API_DATA_TYPE_ERROR];
            failBlock (error,requestInfoModel);
            return;
        }
    }
    return;
}

- (void)handleSpecialRequestSuccess:(NSDictionary *)dictionary
                       requestModel:(RequestInfoModel *)requestInfoModel
                           sucBlock:(NetworkRequestSuccessBlock)sucBlock
                          failBlock:(NetworkRequestFailureBlock)failBlock{
    
    if (!dictionary || [[dictionary allKeys] count]==0) {
        if (failBlock) {
            NSError *error = [self errorWithErrorInfo:@"网络不给力" domain:@"网络不给力" code:RESULT_API_DATA_TYPE_HTTPERROR];
            failBlock (error,requestInfoModel);
        }
        return;
    }
    
    NSString *errorCode = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"errorCode"]];
    if (![dictionary objectForKey:@"errorCode"]) {
        errorCode = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"code"]];
    }
    if ([errorCode isEqualToString:@"1"]) {
        if (dictionary) {
            if (sucBlock){
                sucBlock (dictionary,requestInfoModel);
            }
        }else{
            if (failBlock) {
                NSError *error = [self errorWithErrorInfo:@"数据错误" domain:@"数据错误" code:RESULT_API_DATA_TYPE_ERROR];
                failBlock (error,requestInfoModel);
            }
        }
        return;
    }
    
    //其它异常：
    if (dictionary && ([dictionary objectForKey:@"errorCode"] || [dictionary objectForKey:@"code"] || [dictionary objectForKey:@"errorMessage"] || [dictionary objectForKey:@"msg"] || [dictionary objectForKey:@"message"])) {
        if (failBlock) {
            NSString *errorMessage = nil;
            if ([dictionary objectForKey:@"errorMessage"]) {
                errorMessage = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"errorMessage"]];
            }
            if ([dictionary objectForKey:@"msg"]) {
                errorMessage = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"msg"]];
            }
            if ([dictionary objectForKey:@"message"]) {
                errorMessage = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"message"]];
            }
            if (!ValidStr(errorMessage)) {
                errorMessage = @"网络不给力";
            }
            NSError *error = [self errorWithErrorInfo:errorMessage domain:errorMessage code:RESULT_API_DATA_TYPE_ERROR];
            failBlock (error,requestInfoModel);
            return;
        }
    }
    return;
}





- (void)handleRequestFailed:(NSError *)error
               requestModel:(RequestInfoModel *)requestInfoModel
                  failBlock:(NetworkRequestFailureBlock)failBlock{
    
    //    NSLog(@"失败：requestInfoModel.requestParam==%@",requestInfoModel.requestParam);
    
    if (failBlock) {
        // 用户手动中断请求，一般会在离开界面的时候才会终止,故无需抛出错误
        if (error.code == -999) {
            [[BYToastTool share] hide];
            return;
        }
        
        NSError *customError;
        if (![[self class] isConnectNetwork]) {
            customError = [self errorWithErrorInfo:@"网络连接超时，请稍后重试！" domain:@"" code:RESULT_API_DATA_TYPE_HTTPERROR];
        }else if (error.code == 1){
            customError = [self errorWithErrorInfo:@"网络连接超时，请稍后重试！" domain:@"" code:RESULT_API_DATA_TYPE_HTTPERROR];
        }else if (error.code == 2){
            customError = [self errorWithErrorInfo:@"网络连接超时，请稍后重试！" domain:@"" code:RESULT_API_DATA_TYPE_HTTPERROR];
        }else{
            NSString *errorInfo = [OCBaseService errorWithCode:error.code];
            customError = [self errorWithErrorInfo:errorInfo domain:error.domain code:error.code];
        }
        failBlock (customError,requestInfoModel);
    }
}

//2016年07月14日 add by 胡仕君：精简代码添加的方法
- (NSError *) errorWithErrorInfo:(NSString *) errorInfo domain:(NSString *)domain code:(NSInteger)code {
    NSDictionary *userInfo = [NSDictionary dictionaryWithObject:errorInfo
                                                         forKey:PL_ERROR_INFO_KEY];
    NSError *resultError = [[NSError alloc] initWithDomain:domain
                                                      code:code
                                                  userInfo:userInfo];
    return resultError;
}




- (void)handleShopCartRequestSuccess:(NSDictionary *)dictionary
                        requestModel:(RequestInfoModel *)requestInfoModel
                            sucBlock:(NetworkRequestSuccessBlock)sucBlock
                           failBlock:(NetworkRequestFailureBlock)failBlock{
    
    if (!dictionary || [[dictionary allKeys] count]==0) {
        if (failBlock) {
            NSError *error = [self errorWithErrorInfo:@"网络不给力" domain:@"网络不给力" code:RESULT_API_DATA_TYPE_HTTPERROR];
            failBlock (error,requestInfoModel);
        }
        return;
    }
    
    NSString *errorCode = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"code"]];
    
    if ([errorCode isEqualToString:@"0"]) {
        if (dictionary) {
            if (sucBlock){
                sucBlock (dictionary,requestInfoModel);
            }
        } else {
            if (failBlock) {
                NSError *error = [self errorWithErrorInfo:@"数据错误" domain:@"数据错误" code:RESULT_API_DATA_TYPE_ERROR];
                failBlock (error,requestInfoModel);
            }
        }
        return;
    }
    
    //其它异常：
    if (dictionary && ([dictionary objectForKey:@"code"] || [dictionary objectForKey:@"message"])) {
        if (failBlock) {
            NSString *errorMessage = nil;
            if ([dictionary objectForKey:@"message"]) {
                errorMessage = [NSString stringWithFormat:@"%@",[dictionary objectForKey:@"message"]];
            }
            if (!ValidStr(errorMessage)) {
                errorMessage = @"网络不给力";
            }
            NSError *error = [self errorWithErrorInfo:errorMessage domain:errorMessage code:RESULT_API_DATA_TYPE_ERROR];
            failBlock (error,requestInfoModel);
            return;
        }
    }
    return;
}



@end
